﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Web.Security;

namespace SEP.Admin
{
    public partial class CodeTableManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            if(!IsPostBack)
            {
                BindInfo();
                ClearTextBoxes();
            }
        }

        private void BindInfo()
        {
            grvAddresses.DataSource = AddressType.GetAll();
            grvAddresses.DataBind();

            grvEmailAddresses.DataSource = EmailType.GetAll();
            grvEmailAddresses.DataBind();

            grvPhones.DataSource = PhoneType.GetAll();
            grvPhones.DataBind();            
        }

        private void ClearTextBoxes()
        {
            txtDescriptionAddress.Text = string.Empty;
            txtDescriptionEmail.Text = string.Empty;
            txtDescriptionPhone.Text = string.Empty;
        }

        protected void lnkAddPhone_Click(object sender, EventArgs e)
        {
            string desc = txtDescriptionPhone.Text;
            string msg = "Record successfully added.";

            if(!string.IsNullOrWhiteSpace(desc))
            {
                PhoneType phoneType = new PhoneType();
                phoneType.Description = desc;
                
                try
                {
                    phoneType.Create();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }                
            }
            else
            {
                msg = "You must enter a description.";
            }

            lblMessage.Text = msg;

            BindInfo();
        }

        protected void lnkAddAddress_Click(object sender, EventArgs e)
        {
            string desc = txtDescriptionAddress.Text;
            string msg = "Record successfully added.";

            if (!string.IsNullOrWhiteSpace(desc))
            {
                AddressType addressType = new AddressType();
                addressType.Description = desc;

                try
                {
                    addressType.Create();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            else
            {
                msg = "You must enter a description.";
            }

            lblMessage.Text = msg;

            BindInfo();
        }

        protected void lnkAddEmail_Click(object sender, EventArgs e)
        {
            string desc = txtDescriptionEmail.Text;
            string msg = "Record successfully added.";

            if (!string.IsNullOrWhiteSpace(desc))
            {
                EmailType emailType = new EmailType();
                emailType.Description = desc;

                try
                {
                    emailType.Create();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            else
            {
                msg = "You must enter a description.";
            }

            lblMessage.Text = msg;

            BindInfo();
        }
    }
}